<?php
defined('IN_SITE') or exit('Access Denied');

require_once 'adwords.class.php';
$adwords = new adwords();
require_once MOD_ROOT.'attachment/attachment.class.php';
$attachment = new attachment($module, $catid);
$C = subcat('adwords');

switch($action)
{
    default:
		
		if($adwordsid){
			
			require_once 'adwords.class.php';
			$adwords = new adwords();
			$r = $adwords->get($adwordsid);
			
			if(!$r) return false;
			$clicks = $r['clicks'] + 1;
			$db->query("UPDATE `".DB_PRE."adwords` SET `clicks`=$clicks WHERE `adwordsid`=$adwordsid");
			
			header("Location: $r[linkurl]");
			
		
		}
		else{
			$head['title'] = 'xx_'.$SETTING['sitename'];
			$head['keywords'] = $meta_keywords;
			$head['description'] = $meta_description;
			
			$ttl = $child == 1 ? CACHE_PAGE_CATEGORY_TTL : CACHE_PAGE_LIST_TTL;
			header('Last-Modified: '.gmdate('D, d M Y H:i:s', TIME).' GMT');
			header('Expires: '.gmdate('D, d M Y H:i:s', TIME + $ttl).' GMT');
			header('Cache-Control: max-age='.$ttl.', must-revalidate');
			include template('index.html','adwords');
		}
	break; 
	
	case 'tag':
		$keyword = load('keyword.class.php','keyword');
		$data = $keyword->get($tag);
		if(!$data) showmessage("TAG: $tag 不存在！");
		$keyword->hits($tag);
		extract($data);
		
		// 所有tag标签连成的字符串，用于sql查询的时候where in()
		$data2 = $db->select("select `tag` from table_adwords_tag t");
		foreach($data2 as $k=>$v){
			$tags .= !$tags ? $v['tag'] : ','.$v['tag'];
		}
		
		$head['title'] = $tag.'-'.$SETTING['sitename'];
		
		include template('tag.html');
	break;
	
	case 'add':
		if($dosubmit)
		{
			$info['catid'] = $catid;
			$info['status'] = 99;
			if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
			$adwordsid = $adwords->add($info,$cat_selected);
			if($adwordsid) showmessage('发布成功！', url("?module=$module&action=add"));
		}
		else{
			$modelid = 55;
			require_once 'form.class.php';
			require 'model/adwords_form.class.php';
			$adwords_form = new adwords_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $adwords_form->get($data);
			
			//dump($forminfos);
			include template('add.html','adwords');
		}
	break;
	
	// 点击率，调用方法
	//<script language="JavaScript" src="?module=codex&action=count&codexid={$codexid}">...
	case 'count':
		if($adwordsid)
		{
			$adwordsid = intval($adwordsid);
			$r = $db->get_one("SELECT * FROM `".DB_PRE."adwords` WHERE `adwordsid`=$adwordsid");
			if(!$r) return false;
			$views = $r['views'] + 1;
			return $db->query("UPDATE `".DB_PRE."adwords` SET `views`=$views WHERE `adwordsid`=$adwordsid");
			
		}
		//echo "\$('#hits').html($hits)";
	break;
	
	case 'click':
		if($adwordsid)
		{
			$adwordsid = intval($adwordsid);
			$r = $db->get_one("SELECT * FROM `".DB_PRE."adwords` WHERE `adwordsid`=$adwordsid");
			if(!$r) return false;
			$hits = $r['hits'] + 1;
			return $db->query("UPDATE `".DB_PRE."adwords` SET `hits`=$hits WHERE `adwordsid`=$adwordsid");
			
		}
		//echo "\$('#hits').html($hits)";
	break;
	
}
?>